package cn.swu.todolist

import android.annotation.SuppressLint
import android.content.ContentValues
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.ArrayList
import java.util.zip.Inflater


class todolist : AppCompatActivity() {
    lateinit var helper:DBhelper
    lateinit var adapter: todoadapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_todolist)
        init()

        findViewById<Button>(R.id.btnadd).setOnClickListener()
        {
            btnadd()
        }
    }

private fun init()
{
    helper= DBhelper(this,"todolist",1)
    adapter=todoadapter()
    val recycler = findViewById<RecyclerView>(R.id.recycler)
    recycler.layoutManager = LinearLayoutManager(this)
    recycler.adapter = adapter

}
    private fun btnadd() {

        val text = findViewById<EditText>(R.id.listtext).toString()
        println("@@here1!!!")
        helper = DBhelper(this, "listdb", 1)
        println("@@here2!!!")
        val db = helper.writableDatabase
        val value = ContentValues().apply {
            put("content", text)
        }
        val rs = db.insert("todolist", null, value)
println("@@here!!!")
        Toast.makeText(this, if (rs < 0) "保存失败" else "保存成功", Toast.LENGTH_LONG).show()
        showdb()
    }

    @SuppressLint("Range")
    private fun showdb() {
        val array = arrayListOf<Todo>()
        val helper = DBhelper(this, "listdb", 1)
        val db = helper.readableDatabase
        val cursor = db.query("todolist", null, null, null, null, null, null)
        if (cursor.moveToFirst()) {
            do {
                array.add(
                    Todo(
                        cursor.getString(cursor.getColumnIndex(Todo.COL_CONTENT)),
                        cursor.getLong(cursor.getColumnIndex(Todo.COL_TIME)),
                    ).apply {
                        id = cursor.getInt(cursor.getColumnIndex(Todo.COL_ID))
                    }
                )
            } while (cursor.moveToNext())
        }
        adapter.setData(array)
        cursor.close()
    }

        // adapter.setData(arr)

//    private fun btnadd() {
//        AlertDialog.Builder(this)
//            .setTitle("ddl终结者~")
//            .setMessage("请输入你的新计划")
//            //.setView()
//            //.setPositiveButton("冲！")
//            .setNegativeButton("点错了~"){d,w->Toast.makeText(this,"exit sucessfully",Toast.LENGTH_SHORT).show()}
//            .show()
//
//    }




    inner class todoadapter: RecyclerView.Adapter<todoviewholder>()
    {
        val data  = arrayListOf<Todo>()
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): todoviewholder {
            val view=LayoutInflater.from(parent.context).inflate(R.layout.listitem,parent,false)
           // val view = LayoutInflater.from(parent.context).inflate(R.layout.item_todo,parent,false)
            return todoviewholder(view).apply {
                id = view.findViewById(R.id.id)
                content = view.findViewById(R.id.listtext)
               // btnUpdate = view.findViewById(R.id.btn_update)
                btnDelete = view.findViewById(R.id.del)
            }
        }

        override fun onBindViewHolder(holder: todoviewholder, position: Int) {
            holder.render(data[position])

        }

        override fun getItemCount(): Int {
            return data.size
        }


        fun setData(array: ArrayList<Todo>) {
            data.addAll(array)
            notifyDataSetChanged()
        }
    }
     inner class todoviewholder(view: View): RecyclerView.ViewHolder(view){

        var id: TextView? = null
        var content: TextView? = null

        var btnDelete: TextView? = null
        var btnUpdate: TextView? = null

        fun render(todo: Todo) {
            id?.text = todo.id.toString()
            content?.text = todo.content

            btnDelete?.setOnClickListener {

            }
            btnDelete?.setOnClickListener {

            }


        }

    }

}